package com.sunyur.tailor.channel.service.middle.rocketmq;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 *
 * @Author 99288
 * @Description mdm消费者
**/
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.mdm.group}", topic = "${rocketmq.consumer.mdm.topic}")
@ConditionalOnProperty(name = "rocketmq.switch", havingValue = "open")
public class MdmConsumer implements RocketMQListener<MessageExt> {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(MdmConsumer.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(MdmConsumer.class);

    @Override
    public void onMessage(MessageExt messageExt) {
        LOGGER_INFO.info("mdm RocketMQ param:{}", JSON.toJSONString(messageExt));
        try {
            String msgBody = new String(messageExt.getBody(), StandardCharsets.UTF_8);
            LOGGER_INFO.info("mdm RocketMQ body:{}", msgBody);

        } catch (Exception e) {
            LOGGER_ERROR.error("mdm RocketMQ exception:{}", e);
        }
    }
}